package com.lzx.test;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMPTest2 {
	
	public static void main(String[] args) throws Exception{
		Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161");  
		   TransportMapping transport = new DefaultUdpTransportMapping();  
		   Snmp snmp = new Snmp(transport);  
		   USM usm = new USM(SecurityProtocols.getInstance(),  
		                     new OctetString(MPv3.createLocalEngineID()), 0);  
		   SecurityModels.getInstance().addSecurityModel(usm);  
		   transport.listen();  
		   
		   
		   
		   
		   
		// add user to the USM  
		   snmp.getUSM().addUser(new OctetString("MD5DES"),  
		                         new UsmUser(new OctetString("MD5DES"),  
		                                     AuthMD5.ID,  
		                                     new OctetString("MD5DESUserAuthPassword"),  
		                                     PrivDES.ID,  
		                                     new OctetString("MD5DESUserPrivPassword")));  
//		   // create the target  
//		   UserTarget target = new UserTarget();  
//		   target.setAddress(targetAddress);  
//		   target.setRetries(1);  
//		   target.setTimeout(5000);  
//		   target.setVersion(SnmpConstants.version3);  
//		   target.setSecurityLevel(SecurityLevel.AUTH_PRIV);  
//		   target.setSecurityName(new OctetString("MD5DES"));  
//		  
//		   // create the PDU  
//		   PDU pdu = new ScopedPDU();  
//		   pdu.add(new VariableBinding(new OID("1.3.6")));  
//		   pdu.setType(PDU.GETNEXT);  
//		  
//		   // send the PDU  
//		   ResponseEvent response = snmp.send(pdu, target);  
//		   // extract the response PDU (could be null if timed out)  
//		   PDU responsePDU = response.getResponse();  
//		   // extract the address used by the agent to send the response:  
//		   Address peerAddress = response.getPeerAddress();  
////		 An asynchronous SNMPv1 request is sent by the following code:   
		  
		   // setting up target  
		   CommunityTarget target = new CommunityTarget();  
		   target.setCommunity(new OctetString("public"));  
		   target.setAddress(targetAddress);  
		   target.setRetries(2);  
		   target.setTimeout(1500);  
		   target.setVersion(SnmpConstants.version1);  
		   // creating PDU  
		   PDU pdu = new PDU();  
		   pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,1})));  
		   pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,2})));
		   pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,2,2,0})));
		   pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.25.2.2.0")));
		   pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.25.2.1.1")));
		   pdu.setType(PDU.GETNEXT);  
		   // sending request  
		   ResponseListener listener = new ResponseListener() {  
		     public void onResponse(ResponseEvent event) {  
		       // Always cancel async request when response has been received  
		       // otherwise a memory leak is created! Not canceling a request  
		       // immediately can be useful when sending a request to a broadcast  
		       // address.  
//		       ((Snmp)event.getSource()).cancel(event.getRequest(), this);  
		       System.out.println("Received response PDU is: "+event.getResponse());  
		       System.out.println("size:" +  event.getResponse().size());
		       System.out.println(event.getRequest().get(0).getVariable());
		     }  
		   };  
		   snmp.sendPDU(pdu, target, null, listener);  
		   
		//Traps (notifications) and other SNMP PDUs can be received by adding the folling code to the first code snippet above:   
		   CommandResponder trapPrinter = new CommandResponder() {  
		     public synchronized void processPdu(CommandResponderEvent e) {  
		       PDU command = e.getPDU();  
		       if (command != null) {  
		         System.out.println(command.toString());  
		       }  
		     }  
		   };  
		   snmp.addCommandResponder(trapPrinter);  
	}

}
